<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper('cookie');
		global $user_cookie;
		$user_cookie = $this->input->cookie('bv_user_cookie');
		if (!$user_cookie) {
			$cookie = array(
					'name'   => 'user_cookie',
					'value'  => md5(rand(1, 100000)),
					'expire' => time() + 8650000,
					'domain' => '',
					'path'   => '/',
					'prefix' => 'bv_',
					'secure' => FALSE
			);

			$this->input->set_cookie($cookie);
		}
		
		//Check if there is exists user with this cookie
		$exists_user = $this->db->query("SELECT * FROM user WHERE user_cookie = '$user_cookie'");
		if ($exists_user->num_rows() > 0) {
			//load setting for this user
			if ($exists_user->row(0)->user_username == "") {
			//If user had had an account.
			
			} else {
			//Anonymous user, give them a new cookie

			}
		} else {
		//First time user, give them new cookir - start to auto analyst
			$this->db->query("INSERT INTO  `user` (`user_id` ,`user_username` ,`user_password` ,`user_email` ,`user_cookie`) VALUES (NULL ,  '',  '',  '',  '$user_cookie');");
		}

	}
	public function index()
	{
	
		
		global $user_cookie;
		$user_favo = $this->db->query("SELECT ucs.* FROM user_cat_select ucs, user u WHERE ucs.user_id = u.user_id AND u.user_cookie = '$user_cookie'");
		
		$user_cat = array();
		foreach ($user_favo->result() as $row) {
			$user_cat[] = $row->cat_id;
		}
		if (count($user_cat) == 0) { 
			$user_cat = array(1,2,3,4,38,36);
		}
	
		$query = $this->db->query("SELECT f.*, c.cat_name, s.source_name FROM feed f, category c, source s WHERE f.feed_cat = c.cat_id AND f.source_id = s.source_id AND c.cat_id IN (".implode(",", $user_cat).")");
		$data = $query->result();
		$cats = array();
		//init
		foreach ($data as $d) {
			$cats[$d->feed_cat] = array();
		}

		foreach ($cats as $cat_id => $cc) {
			$e_f = array(); //$e_s[cat_id] = array of exists feed_id
			$count = 0;
			
			while ($count < 5) {
				$e_s = array(); //$e_s[cat_id] = array of exists source_id
				for ($i = 0; $i < count($data); $i++) {
					$source_id = $data[$i]->source_id;
					$data_cat_id = $data[$i]->feed_cat;
					$feed_id = $data[$i]->feed_id;
					if ($data_cat_id == $cat_id && $count < 5) {
						if (!isset($e_f[$cat_id]) || (!@in_array($feed_id, $e_f[$cat_id]))) { // make sure there is no dupllicate feed_id
							if (!isset($e_s[$cat_id]) || (!@in_array($source_id, $e_s[$cat_id]))) { // make sure there is no continue same source
								$cats[$cat_id][] = $data[$i];
								$e_s[$cat_id][] = $source_id;
								$e_f[$cat_id][] = $feed_id;
								$count++;
								if ($count == 5) $e_s = array();
								//file_put_contents('a.txt', file_get_contents('a.txt') . $count . "-" . $source_id . "\r\n");
							}
						}
					}
				} //end for
			}
		}
		
		$categories = $this->db->query("SELECT * FROM category");
		$data['categories'] = $categories->result();
		
		$sources = $this->db->query("SELECT * FROM source");
		$data['sources'] = $sources->result();
		
		$source_as_cat = array();
		foreach ($data['categories'] as $c) {
			$query = $this->db->query("SELECT s.source_id, r.cat_id, s.source_name FROM rsslink r, source s WHERE r.source_id = s.source_id AND r.cat_id = ".$c->cat_id);
			foreach ($query->result() as $row) {
				$source_as_cat[$c->cat_id][] = $row;
			}
		}
		$data['source_as_cat'] = $source_as_cat;
		
		$data['data'] = $cats;

		$data['user_cookie'] = $user_cookie;
		$data['user_cat'] = $user_cat;
		$this->load->view('home_view.php', $data);
	}
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */